package com.healthbox.appointmentservice.controller;

import com.healthbox.appointmentservice.DTO.LimitQueryDTO;
import com.healthbox.appointmentservice.DVO.AppointmentInfoDVO;
import com.healthbox.appointmentservice.DVO.AppointmentLimitDVO;
import com.healthbox.appointmentservice.common.Result;
import com.healthbox.appointmentservice.service.AppointmentInfoModifyService;
import com.healthbox.appointmentservice.service.AppointmentInfoQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author pengpeng
 * @date 2022/5/8
 */
@RestController
@RequestMapping(path = "/appointments/info")
@Api(tags = "预约管理")
@Slf4j
public class AppointmentInfoController {
    @Autowired
    private AppointmentInfoQueryService infoQueryService;
    @Qualifier("appointmentInfoModifyProxy")
    @Autowired
    private AppointmentInfoModifyService infoModifyService;

    @ApiOperation(value = "用户查看可预约时间段", notes = "可以根据机构名称和检测日期进行删选")
    @GetMapping
    public Result<List<AppointmentLimitDVO>> getAppointmentsOfHospital(
            LimitQueryDTO query,
            @RequestHeader(value = "user-phone") @NotNull String userPhone
    ) {
        log.info("用户查看可预约时间段" + query);
        return Result.ok(infoQueryService.queryAppointmentLimit(query));
    }

    @ApiOperation(value = "用户预约", notes = "对指定检测机构的指定特定检测时间段进行预约，预约成功后会返回预约成功信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user-phone", value = "用户手机号", type = "header", example = "19966668888"),
            @ApiImplicitParam(name = "aLId", value = "核酸检测限制编号, appointment limit id", type="param", example = "1")
    })
    @PutMapping
    public Result<AppointmentInfoDVO> newAppointmentInfo(
            @RequestHeader(value = "user-phone") @NotNull String userPhone,
            @RequestParam @NotNull Long aLId
            ) {
        log.info("用户添加一个预约: userPhone=" + userPhone + " aLid="+ aLId);

        List<AppointmentInfoDVO> exist = infoQueryService.queryHistory(userPhone, null, aLId);
        if (exist==null) {
            log.error("预约出错: userPhone="+ userPhone+", aLid=" + aLId);
            return Result.badRequest("未知错误");
        }
        if (exist.size()>=1) {
            log.warn("用户重复预约");
            return Result.badRequest("不能重复预约");
        }
        AppointmentInfoDVO appointmentInfoDVO = infoModifyService.addAppointment(userPhone, aLId);
        return appointmentInfoDVO==null? Result.badRequest() : Result.ok(appointmentInfoDVO);
    }

    @ApiOperation(value = "用户查看历史预约信息列表", notes = "用户可以对历史预约记录的进行查询")
    @ApiImplicitParam(name = "user-phone", value = "用户手机号", paramType = "header")
    @GetMapping("/histories")
    public Result<List<AppointmentInfoDVO>> getAppointmentsOfUser(@RequestHeader(value = "user-phone") @NotNull String userPhone) {
        log.info(userPhone + " 查询预约历史");
        return Result.ok(infoQueryService.queryHistory(userPhone, null, null));
    }

    @ApiOperation(value = "用户查看是否已预约", notes = "用户可以查询是否已预约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user-phone", value = "用户手机号", paramType = "header"),
            @ApiImplicitParam(name = "aLId", value = "检测机构Id", paramType = "path")
    })
    @GetMapping("/{aLId}")
    public Result<Boolean> existsAppointment(
            @RequestHeader(value = "user-phone") @NotNull String userPhone,
            @PathVariable @NonNull Long aLId
    ) {
        log.info("查询是否预约某个检测时间段: userPhone=" + userPhone + ", appointmentLimit id=" + aLId);

        List<AppointmentInfoDVO> exist = infoQueryService.queryHistory(userPhone, null, aLId);
        if (exist==null) {
            log.error("查询是否存在出错: userPhone="+ userPhone+", aLid=" + aLId);
            return Result.badRequest("服务器未知错误");
        }
        if (exist.size() == 1)  {
            return Result.ok(true);
        }
        return Result.ok(false);
    }
}
